<!-- 新增检测位置 -->
<template>
  <div id="dialog-container">
    <el-dialog v-dialogDrag title="新增检测位置" :fullscreen="isFullScreen" :custom-class="isFullScreen?'fullscreen-default-dialog':'vertical-center-dialog'" append-to-body :close-on-click-modal="false" :visible.sync="dialogVisible" @close="$emit('update:show', false)" :before-close="handleClose" :show="show" width="800px">
      <button type="button" aria-label="fullScreen" class="el-dialog__headerbtn" style="margin-right:25px;top:" @click="isFullScreen = !isFullScreen">
                                                                                                  <i class="el-dialog__close iconfont icon-fuzhicopy22"></i>
                                                                                                </button>
      <el-form :model="formData" :rules="rules" :inline="true" ref="form" label-width="200px" size="small">
        <el-row>
          <el-col :span="20">
            <el-form-item label="检测位置代码：" prop="code">
              <el-input v-model="formData.code":disabled="canEdit"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="检测位置中文：" prop="name">
              <el-input  v-model="formData.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="verifyAdd">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  postsaveDict,
  getDictByCode
} from "service/task";
export default {
  data() {
    return {
      canEdit: false,
      conditionList: [],
      dialogVisible: false,
      isFullScreen: false,
      formData: {
        code: "",
        name: ""
      },
      rules: {
        code: [
          {
            required: true,
            message: "请输入检测位置代码",
            trigger: "blur"
          }
        ],
        name: [
          {
            required: true,
            message: "请输入检测位置中文",
            trigger: "blur"
          }
        ]
      }
    };
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    modifyId: {
      type: String
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show;
      if (this.dialogVisible) {
        if (this.modifyId) {
          this.canEdit=true;
          getDictByCode(this.modifyId).then(res => {
            this.formData = res.data.result;
          });
        }
      }
    }
  },
  methods: {
    handleClose() { /* 关闭的时候更新状态值 */
        this.dialogVisible = false;
        this.modifyId = null;
        this.formData.code = "";
        this.formData.name = "";
         this.canEdit=false;
        this.$refs["form"].clearValidate();

      },
    // 确定
    verifyAdd() {
      //let deviceData = [];
      //for (let i = 0; i < this.tableData.length; i++) {
       // deviceData.push({
         // deviceId: this.tableData[i].id,
         // occupy: this.tableData[i].occupy
       // });
      //}

      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = {
            id: this.modifyId,
            code: this.formData.code,
            name: this.formData.name,
            parentCode: "TEST_POSITION"
          };
          postsaveDict(params).then(res => {
            if (res.data.success) {
             this.$message({
            message: res.data.message,
            type: 'success',
            duration: 1000
            });
              this.$emit('refreshAdd');
              this.dialogVisible = false;
              this.modifyId = null;
              this.formData.code = "";
              this.formData.name = "";
              this.canEdit=false;
            } else {
              this.$alert(res.data.message, "提示", {
                type: "error"
              });
              this.canEdit=false;
              return;
            }
          });
        }
      });
    }
  }
};
</script>
